package reflect;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 通过反射机制实例化对象
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入类名:");
        String className = scan.nextLine();
        Class cls = Class.forName(className);

        Object obj = cls.newInstance();//只能使用无参构造器实例化!
        System.out.println(obj);

        /*
          通过newInstance()可以进行实例化
          假设cls为String对象，则newInstance()的结果相当于String s = new String();
          假设cls为ArrayList对象，则newInstance()的结果相当于ArrayList list = new ArrayList();
         */
        ArrayList list = new ArrayList<>();
        System.out.println(list);

    }
}
